﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BLL;
using Newtonsoft.Json;

namespace Sales
{
    public partial class frmGathering : Form
    {
        private static frmGathering gathering = null;
        private bool hit = false;
        #region 页面属性
        /// <summary>
        /// 应收
        /// </summary>
        public string Ys
        {
            get;
            set;
        }
        /// <summary>
        /// 现金
        /// </summary>
        public string Xj
        {
            get;
            set;
        }
        /// <summary>
        /// 找零
        /// </summary>
        public string Zl
        {
            get;
            set;
        }

        /// <summary>
        /// 欠款
        /// </summary>
        public string Qk
        {
            get;
            set;
        }

        /// <summary>
        /// 会员积分
        /// </summary>
        public decimal Hyjf
        {
            get;
            set;
        }
        /// <summary>
        /// 会员卡号
        /// </summary>
        public string Hykh
        {
            get;
            set;
        }
        /// <summary>
        /// 会员卡余额
        /// </summary>
        public decimal Hykye
        {
            get;
            set;
        }
        public int IsChoice
        {
            get;
            set;
        }
        /// <summary>
        /// 本次积分
        /// </summary>
        public string Bcjf
        {
            get;
            set;
        }

        /// <summary>
        /// 是否敲回车
        /// </summary>
        public bool IsEnter { get; set; }

        public SaleStatus saleEntity
        {
            get; set;
        }
        #endregion

        public frmGathering()
        {
            InitializeComponent();

            this.ctxtZl.ForeColor = Color.Blue;
            this.ctxtQk.ForeColor = Color.Red;

            MiddleModule.eventSend += new Send(MiddleModule_eventSend);
            this.ctxtYs.Font = new Font("宋体", 12, FontStyle.Bold);
            this.txtXj.Font = new Font("宋体", 12, FontStyle.Bold);
            this.ctxtZl.Font = new Font("宋体", 12, FontStyle.Bold);
            this.ctxtQk.Font = new Font("宋体", 12, FontStyle.Bold);

            this.ctxtJfye.Font = new Font("宋体", 12, FontStyle.Bold);
            this.ctxtBcjf.Font = new Font("宋体", 12, FontStyle.Bold);

            this.ctxtHyk.Font = new Font("宋体", 12, FontStyle.Bold);

            this.ctxtXjye.Font = new Font("宋体", 12, FontStyle.Bold);


        }
        void MiddleModule_eventSend(object sender, object receiver, object msg)
        {
            frmMain frmMain = sender as frmMain;

            //应收款$会员余额$会员卡号
            if (null != frmMain && gathering.ToString() == receiver.ToString())
            {
                SaleStatus model = JsonConvert.DeserializeObject<SaleStatus>(msg.ToString());
                if (model != null)
                {

                    saleEntity = model;
                    decimal _ys = model.YS;

                    //是否四舍五入
                    if (this.IsChoice > 0)
                        _ys = Math.Round(_ys, 0, MidpointRounding.AwayFromZero);

                    this.ctxtYs.Text = _ys.ToString("0.00");
                    Ys = _ys.ToString("0.00");


                    this.ctxtQk.Text = ""; //(-decimal.Parse(strYs)).ToString("0.00");
                    this.txtXj.Text = "";

                    this.ctxtWx.Text = "";
                    this.ctxtZfb.Text = "";
                    this.ctxtYhk.Text = "";
                    this.ctxtDjq.Text = "";
                    this.ctxtZl.Text = "0.00";

                    //会员消费
                    if (model.Hykh != "")
                    {
                        Hyjf = model.Hyjf;// decimal.Parse(strHyjf);//会员积分
                        Hykh = model.Hykh;// strHykh;//会员卡号
                        Hykye = model.Zhye;// decimal.Parse(strZhye);//会员卡余额
                        Bcjf = Ys;//本次积分
                        this.ctxtBcjf.Text = Ys;

                        this.ctxtJfye.Text = (Hyjf + decimal.Parse(Ys)).ToString();
                        this.ctxtHyk.Text = model.Hykh;


                        this.ctxtXjye.Text = Hykye.ToString("0.0");
                        Hyjf = (Hyjf + decimal.Parse(Ys));//当前积分余额
                    }
                    else
                    {
                        Hyjf = 0;
                        Hykh = "";
                        Hykye = 0;
                        Bcjf = "";
                        this.ctxtBcjf.Text = "";

                        this.ctxtJfye.Text = "";
                        this.ctxtHyk.Text = "";
                        this.ctxtXjye.Text = "";

                        Hyjf = 0;
                    }

                }
            }
        }


        public static frmGathering CreateInstance()
        {
            if (null == gathering)
            {
                gathering = new frmGathering();
            }
            return gathering;
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {

        }

        private void gbtncancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtXj_TextChanged(object sender, EventArgs e)
        {
            string xj = this.txtXj.Text;
            if (xj != "" && Ys != "" && xj != "-" && xj != ".")
            {
                decimal dzl = 0;
                decimal dxj = decimal.Parse(xj);
                decimal dys = decimal.Parse(Ys);
                //会员卡余额  Hykye
                dzl = Hykye + dxj - dys;
                if (dzl > 0)
                {
                    this.ctxtZl.Text = dzl.ToString("0.0");
                    this.ctxtQk.Text = "0.0";
                }
                else
                {
                    this.ctxtQk.Text = dzl.ToString("0.0");

                    this.ctxtZl.Text = "0.0";
                }

            }
            else
            {
                this.ctxtQk.Text = (-decimal.Parse(Ys)).ToString();
                this.ctxtZl.Text = "0.0";
            }
        }

        private void txtXj_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != '-')
            {
                e.Handled = true;
            }
        }
        #region 热键设置

        private void frmGathering_Activated(object sender, EventArgs e)
        {
            HotKey.RegisterHotKey(Handle, 105, HotKey.KeyModifiers.None, Keys.Enter);
            //设置光标位置
            this.txtXj.Focus();
        }

        private void frmGathering_Leave(object sender, EventArgs e)
        {
            //注销Id号为105的热键设定
            HotKey.UnregisterHotKey(Handle, 105);
        }




        private void KeyDownEnter()
        {

            decimal dys = 0;//应收金额
            decimal dss = 0;
            if (Ys != "")
                dys = decimal.Parse(Ys);
            Sale model = Calc();

            if (model.Ss != "")
                dss = decimal.Parse(model.Ss);
            decimal dzk = 0;

            string zk = this.ctxtQk.Text;
            if (zk != "")
                dzk = decimal.Parse(zk);

            model.DZk = dzk;
            model.Hyjf = Hyjf;
            model.Zhye = Hykye;
            model.Bcjf = Bcjf;
            model.Hykxf = model.SsJe;
            model.Zk = zk;
            if (saleEntity != null && saleEntity.TH)
            {
                if (dys > 0 && dss > 0 && dss >= (dys - dzk))
                {

                    string jsonGroup = JsonConvert.SerializeObject(model);

                    frmMain main = new frmMain();
                    MiddleModule.SendMessage(this, main, jsonGroup);
                    this.Close();
                }
                else
                {
                    this.IsEnter = true;
                    MessageBox.Show("输入金额必须等于应收金额！");
                }
            }
            else
            {
                if (dys > 0 && dss >= 0 && (dss + model.Zhye) >= (dys - dzk))
                {
                    if ((dys - dzk) > dss)
                        model.BeYe = true;
                    decimal _hykxf = 0;
                    //会员卡余额 参与消费
                    if (model.Zhye > 0)
                        _hykxf = dys - dzk - dss;//

                    model.Hykxf = _hykxf.ToString();//应收金额-折扣金额-实收金额 =会员卡消费
                    model.Zhye = (model.Zhye - _hykxf);//余额-本次会员卡消费
                    if (model.Zhye < 0)
                        model.Zhye = 0;
                    string jsonGroup = JsonConvert.SerializeObject(model);

                    frmMain main = new frmMain();
                    MiddleModule.SendMessage(this, main, jsonGroup);
                    this.Close();
                }
                else
                {
                    this.IsEnter = true;
                    MessageBox.Show("输入金额必须等于应收金额！");
                }
            }
        }


        //private void KeyDownEnter()
        //{
        //    string xj = this.txtXj.Text.Trim();

        //    //会员卡消费

        //    if (Hykh != "")
        //    {
        //        decimal dys = decimal.Parse(Ys);
        //        decimal dxj = 0;
        //        decimal allje = 0;

        //        decimal hykxf = 0;

        //        if (xj != "")
        //        {
        //            dxj = decimal.Parse(xj);
        //        }
        //        allje = dxj + Hykye;

        //        if (Hykye >= dys)
        //        {
        //            Hykye = Hykye - dys;

        //            hykxf = dys;
        //            decimal dzl = allje - dys;

        //            Zl = "0.0";
        //            Qk = "0.0";

        //            Xj = xj;
        //            string msg = Xj + "," + Qk + "," + Zl + "," + Hyjf + "," + Hykye.ToString() + "," + Bcjf + "," + Ys + "," + hykxf.ToString();
        //            frmMain main = new frmMain();
        //            MiddleModule.SendMessage(this, main, msg);
        //            this.Close();

        //        }
        //        else if (dys > Hykye && dxj == 0)
        //        {
        //            int key = CloseWindow.FindMessageBox("缺款警告");
        //            if (key == 0)
        //            {
        //                hit = true;
        //                Message.MsgBoxWarn("会员卡余额不足请现金交易或者充值！", "缺款警告");
        //            }

        //        }
        //        else if (dys > Hykye && allje < dys)
        //        {
        //            int key = CloseWindow.FindMessageBox("缺款警告");

        //            if (key == 0)
        //            {
        //                hit = true;
        //                if (Message.AskQuestion("现金不足是否允许消费？", "缺款警告"))
        //                {
        //                    hykxf = Hykye;
        //                    Hykye = 0;
        //                    decimal dzl = allje - dys;

        //                    if (dzl > 0)
        //                    {
        //                        Zl = dzl.ToString("0.0");
        //                        Qk = "0.0";
        //                    }
        //                    else
        //                    {
        //                        Qk = dzl.ToString("0.0");
        //                        Zl = "0.0";
        //                    }

        //                    Xj = xj;
        //                    //string msg = Xj + "," + Qk + "," + Zl + "," + Hyjf + "," + Hykye.ToString();
        //                    string msg = Xj + "," + Qk + "," + Zl + "," + Hyjf + "," + Hykye.ToString() + "," + Bcjf + "," + allje.ToString() + "," + hykxf.ToString();//非常情况下 营业员收取了 比应收款少的现金(现金+会员卡余额)
        //                    frmMain main = new frmMain();
        //                    MiddleModule.SendMessage(this, main, msg);
        //                    this.Close();
        //                }
        //            }
        //        }
        //        else if (allje >= dys)
        //        {
        //            hykxf = Hykye;

        //            Hykye = 0;

        //            decimal dzl = allje - dys;

        //            if (dzl > 0)
        //            {
        //                Zl = dzl.ToString("0.0");
        //                Qk = "0.0";
        //            }
        //            else
        //            {
        //                Qk = dzl.ToString("0.0");
        //                Zl = "0.0";
        //            }

        //            Xj = xj;
        //            //  string msg = Xj + "," + Qk + "," + Zl + "," + Hyjf + "," + Hykye.ToString();
        //            string msg = Xj + "," + Qk + "," + Zl + "," + Hyjf + "," + Hykye.ToString() + "," + Bcjf + "," + Ys + "," + hykxf.ToString();
        //            frmMain main = new frmMain();
        //            MiddleModule.SendMessage(this, main, msg);
        //            this.Close();
        //        }
        //    }
        //    else
        //    {
        //        //普通消费
        //        if (xj != "")
        //        {
        //            decimal dzl = 0;
        //            decimal dxj = decimal.Parse(xj);
        //            decimal dys = decimal.Parse(Ys);
        //            dzl = dxj - dys;

        //            if (dzl > 0)
        //            {
        //                Zl = dzl.ToString("0.0");
        //                Qk = "0.0";
        //            }
        //            else
        //            {
        //                Qk = dzl.ToString("0.0");
        //                Zl = "0.0";
        //            }

        //            Xj = xj;
        //            // string msg = Xj + "," + Qk + "," + Zl + "," + Hyjf + "," + Hykye.ToString();
        //            string msg = Xj + "," + Qk + "," + Zl + "," + Hyjf + "," + Hykye.ToString() + "," + Bcjf;
        //            frmMain main = new frmMain();
        //            MiddleModule.SendMessage(this, main, msg);
        //            this.Close();
        //        }
        //    }
        //}



        /// 重载FromA中的WndProc函数
        /// 监视Windows消息
        /// 重载WndProc方法，用于实现热键响应
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            const int WM_HOTKEY = 0x0312;
            //按快捷键
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    switch (m.WParam.ToInt32())
                    {
                        case 105:
                            if (hit)
                            {
                                CloseWindow.KillMessageBox("缺款警告");
                                hit = false;
                            }
                            else
                            {
                                KeyDownEnter();
                            }
                            break;
                    }
                    break;
            }

            base.WndProc(ref m);
        }
        #endregion

        private void frmGathering_Load(object sender, EventArgs e)
        {

        }

        private void txtXj_TextChanged_1(object sender, EventArgs e)
        {
            Calc();
        }
        private Sale Calc()
        {
            string xj = this.txtXj.Text.Trim();//现金
            string wx = this.ctxtWx.Text.Trim();//微信
            string zfb = this.ctxtZfb.Text.Trim();//支付宝
            string yhk = this.ctxtYhk.Text.Trim();//银行卡
            string djq = this.ctxtDjq.Text.Trim();//代金券
            string hykYe = this.ctxtXjye.Text.Trim();//会员卡余额

            string zk = this.ctxtQk.Text.Trim();//折扣


            decimal dys = 0;


            decimal dxj = 0;//现金
            decimal dwx = 0;//微信
            decimal dzfb = 0;//支付宝
            decimal dyhk = 0;//银行卡
            decimal ddjq = 0;//代金券

            decimal dhykye = 0;//余额

            decimal dzk = 0;
            if (Ys != "")
                dys = decimal.Parse(Ys);
            //普通消费
            if (xj != "")
                dxj = decimal.Parse(xj);
            //微信消费
            if (wx != "")
                dwx = decimal.Parse(wx);
            //支付宝消费
            if (zfb != "")
                dzfb = decimal.Parse(zfb);
            //银行卡消费
            if (yhk != "")
                dyhk = decimal.Parse(yhk);
            //代金券消费
            if (djq != "")
                ddjq = decimal.Parse(djq);
            //会员卡余额
            if (hykYe != "")
                dhykye = decimal.Parse(hykYe);


            if (zk != "")
                dzk = decimal.Parse(zk);



            decimal all = dxj + dwx + dzfb + dyhk + ddjq;//总计现金(包括代金券)

            decimal all_1 = dxj + dwx + dzfb + dyhk;//总计现金

            decimal zl = all - (dys - dzk);
            if (zl > 0)
                this.ctxtZl.Text = zl.ToString();
            else
                this.ctxtZl.Text = "0";


            Sale model = new Sale();
            model.Xj = xj;
            model.Wx = wx;
            model.Yhk = yhk;
            model.Ys = Ys;
            model.Zfb = zfb;
            model.Zk = Qk;
            model.Zl = ctxtZl.Text;

            model.Ss = all.ToString();
            model.SsJe = all_1.ToString();
            model.Zhye = dhykye;//账号余额
            return model;
        }

        private void ctxtWx_TextChanged(object sender, EventArgs e)
        {
            Calc();
        }

        private void ctxtZfb_TextChanged(object sender, EventArgs e)
        {
            Calc();
        }

        private void ctxtYhk_TextChanged(object sender, EventArgs e)
        {
            Calc();
        }

        private void ctxtDjq_TextChanged(object sender, EventArgs e)
        {
            Calc();
        }

        private void gbtnSK_Click(object sender, EventArgs e)
        {
            //   SettleAccounts();
            KeyDownEnter();

        }
    }
}
